var AboutCar = (function($){
	var USER_CALL = {};
	var USER_APPOINTENT = {};
	var queryCost_CallBack = function(data){
		if(data.data == null){
			$("#price").parent(".weui_cell").hide();
			$.toast("此线路暂未开通","forbidden");
		}
		else if(data.code == "200"){
			USER_CALL.cost = data.data.cost;
			Build.saveCache("USER_CALL", "cost", data.data.cost);
			$("#price").text("￥"+data.data.cost).parent(".weui_cell").show();
		}else{
			$.toast("查询失败","forbidden");
		}
	};
	var instantCallCar_CallBack = function(data){
		if(data.code == "200"){
			window.location.href = "page_callCarLoading.html";
		}else if(data.code == "421"){
			$.toast("您有未处理订单,请在行程内查看","forbidden");
		}
		else{
			$.toast("叫车失败","forbidden");
		}
	}
	var queryCost = function(){
		var startLocation = $("#startLoca").text();
		var endLocation = $("#endLoca").text();
		if(endLocation == "您要去哪儿" || startLocation == "您从哪出发"){
			return $("#price").parent(".weui_cell").hide();
		}else if(startLocation == endLocation){
			$.toast("起点终点不能一样","forbidden");
			return $("#price").parent(".weui_cell").hide();
		}else{
			$.showLoading("正在查询价格");
			Build.request({
				type: "get",
				port: "/price/queryPrice",
				data: {
					startLocation: startLocation,
					endLocation: endLocation,
				} 
			} ,queryCost_CallBack, false);
		}
	};
	var chooseWay = function(){
		$("#startLoca").picker({
	  		title: "您从哪儿出发",
		  	cols: [{
			      textAlign: 'center',
			      values: [ "唐山","迁西","迁安","玉田","滦县","滦南","乐亭","京唐港","唐海","曹妃甸","遵化","丰润",
			      		"古冶","丰南","南堡","天津机场","北京机场"]
			    }],
		  	onChange: function(p,v,d){
			 	$("#startLoca").text(v);
			},
			onClose: function(){
				USER_CALL.startLocation = $("#startLoca").text();
				queryCost();
			}
		});
		$("#endLoca").picker({
	  		title: "您要去哪儿",
		  	cols: [{
			      textAlign: 'center',
			      values: [ "唐山","迁西","迁安","玉田","滦县","滦南","乐亭","京唐港","唐海","曹妃甸","遵化","丰润",
			      		"古冶","丰南","南堡","天津机场","北京机场"]
			    }],
		  	onChange: function(p,v,d){
			 	$("#endLoca").text(v);
			},
			onClose: function(){
				USER_CALL.endLocation = $("#endLoca").text();
				queryCost();
			}
		});
		$("#persons").picker({
	  		title: "请选择几人乘车",
		  	cols: [{
			      textAlign: 'center',
			      values: [ "1人","2人","3人","4人","5人"]
			    }],
		  	onChange: function(p,v,d){
			 	$("#persons").text(v + "乘车");
			},
			onClose: function(){
				var pers = $("#persons").text().substr(0,1)
				USER_CALL.passengersNumbers = pers;
			}
		});
		var dateTime = function(){
			var temp = [];
			var date = new Date();
			console.log(date.setDate(date.getDate()+15));
			return	['11.29', '11.30', '12.01', '12.02', '12.03', '12.04', '12.05', '12.06']
		}
		$("#startTime").picker({
		  	title: "您什么时候出发",
		  	cols: [{
		      		textAlign: 'center',
		      		values: dateTime()
			    },{
		      		textAlign: 'center',
		      		values: ['08点', '09点', '10点', '11点', '12点', '13点', '14点','15点','16点','17点','18点']
			    },{
			    	textAlign: 'center',
			    	values: ['00分','05分', '10分','15分','20分','25分','30分','35分','40分','45分','50分','55分']
		    }],
		    onClose: function(e){
		    	var date = new Array();
		    	date[0] = e.displayValue[0].replace(".", "-")+"/";
		    	date[1] = e.displayValue[1].replace("点", ":");
		    	date[2] = e.displayValue[2].replace("分", "");
		    	$("#startTime").text(date.join(""));
		    	USER_APPOINTENT.startTime = date.join("");
		    }
		});
	};
	var turnExactLocation = function(lng, lat){
		$.ajax({
			type:"get",
			dataType: "jsonp",
			url:"http://api.map.baidu.com/geocoder/v2/",
			async:true,
			jsonpCallback: "callback",
			data: {
				location: lat+","+lng,
				output: "json",
				ak: "EF06cfb26173665ad80b8edf6a328192",
			},
			success: function(data){
				USER_CALL.startExactLocation = data.result.formatted_address+ "" +data.result.sematic_description;
				$.alert(data.result.formatted_address+ "" +data.result.sematic_description);
			}
		});
		function callback(){}
	};
	var geolocation = function(){
		var geolocation = new BMap.Geolocation();
		geolocation.getCurrentPosition(function(r){
			if(this.getStatus() == BMAP_STATUS_SUCCESS){
				USER_CALL.location = r.point.lng + "," + r.point.lat;
				turnExactLocation((r.point.lng), (r.point.lat));
			}
			else {
				$.alert("定位失败，请打开您的GPS设置并刷新页面",function(){geolocation();})
			}        
		}, {enableHighAccuracy: true});

	};
	chooseWay();
	geolocation();
	return {
		instantCallCar: function(){
			if($("#endLoca").text() == "您要去哪儿" || $("#persons").text() == "几人乘车"
				|| $("#startLoca").text() == "您从哪出发"){
				return $.toast("请检查您输入信息是否完整","forbidden");
			}
			if(sessionStorage.USER){
				var user = JSON.parse(sessionStorage.USER);				
			}else{
				return $.toast("请先登录","forbidden");
			}
			if($("#price").parent(".weui_cell").css("display") == "none"){
				return $.toast("请重新输入目的地，查询价格","forbidden");
			}
			var data = {
				uid: user.id,
				uid: user.id,
				lastName: user.lastName,
				firstName: user.firstName,
				userPhoneNumbers: user.phoneNumber,
				startLocation: USER_CALL.startLocation,
				endLocation: USER_CALL.endLocation,
				passengersNumbers: USER_CALL.passengersNumbers,
				cost: USER_CALL.cost,
				location: "40.197807,117.973482",
				startExactLocation: "河北省唐山市遵化市文礼东街"
			};
			$.showLoading("正在叫车请等待");
			Build.request({
				type: "get",
				data: data,
				port: "/taxi/callCar"
			}, instantCallCar_CallBack, false);
		},
		appointmentCallCar: function(){
			if($("#endLoca").text() == "您要去哪儿" || $("#persons").text() == "几人乘车"
				|| $("#startLoca").text() == "您从哪出发" || $("#startTime").text() == "什么时候出发"){
				return $.toast("请检查您输入信息是否完整","forbidden");
			}
		}
	}
})(jQuery)